package com.weaving.monitor.config;

import com.weaving.monitor.entity.Machine;
import com.weaving.monitor.entity.PendingPlan;
import com.weaving.monitor.repository.MachineRepository;
import com.weaving.monitor.repository.PendingPlanRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Component
public class DataInitializer implements CommandLineRunner {

    @Autowired
    private MachineRepository machineRepository;

    @Autowired
    private PendingPlanRepository pendingPlanRepository;

    @Override
    public void run(String... args) throws Exception {
        // 清空现有数据
        machineRepository.deleteAll();
        pendingPlanRepository.deleteAll();

        // 初始化25台机器数据
        for (int i = 1; i <= 25; i++) {
            Machine machine = new Machine();
            machine.setMachineNumber(i);
            
            if (i <= 5) {
                // 前5台机器 - 运行中
                machine.setModel("KS-3");
                machine.setFabricLength(850.5);
                machine.setOperator("张三");
                machine.setSpeed(180);
                machine.setL1Pattern("A-001");
                machine.setL1Remaining(25.5);
                machine.setL2Pattern("B-002");
                machine.setL2Remaining(18.2);
                machine.setL3Pattern("C-003");
                machine.setL3Remaining(32.1);
                machine.setL4Pattern("D-004");
                machine.setL4Remaining(15.8);
                machine.setNextPlan("E-005");
                machine.setStatus("RUNNING");
            } else {
                // 其他机器 - 停止状态
                machine.setModel("KS-4");
                machine.setFabricLength(0.0);
                machine.setOperator("李四");
                machine.setSpeed(0);
                machine.setL1Pattern("X-XX-XX-XX");
                machine.setL1Remaining(0.0);
                machine.setL2Pattern("X-XX-XX-XX");
                machine.setL2Remaining(0.0);
                machine.setL3Pattern("X-XX-XX-XX");
                machine.setL3Remaining(0.0);
                machine.setL4Pattern("X-XX-XX-XX");
                machine.setL4Remaining(0.0);
                machine.setNextPlan("XXX-XX");
                machine.setStatus("STOPPED");
            }
            
            machine.setLastUpdated(LocalDateTime.now());
            machineRepository.save(machine);
        }

        // 初始化待翻品种计划数据
        List<String> planDetails = Arrays.asList(
            "A-001-红色-1000m", "B-002-蓝色-800m", "C-003-绿色-1200m",
            "D-004-黄色-600m", "E-005-紫色-900m", "F-006-橙色-750m"
        );

        for (int machineNum = 1; machineNum <= 25; machineNum++) {
            for (int round = 1; round <= 3; round++) {
                PendingPlan plan = new PendingPlan();
                plan.setMachineNumber(machineNum);
                plan.setRoundNumber(round);
                plan.setPlanDetails(planDetails.get((machineNum + round) % planDetails.size()));
                plan.setPriority(round);
                pendingPlanRepository.save(plan);
            }
        }
    }
} 